package com.Test;



/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-12-22 09:52:45
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-12-22     张李鑫                     1.0         1.0 Version
 */
public class Demo01 {
    Object object = new Object();
    int i = 0;


    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();
        demo01.lock();
    }




    Object resource=new Object();
    Object resource2=new Object();


    public void  lock (){
        new Thread(()->{
            synchronized (resource){
                System.out.println("t1 get resource");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource2){
                    System.out.println("t1 get resource2");
                }
            }

        }).start();


        new Thread(()->{
            synchronized (resource2){
                System.out.println("t get resource2");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource){
                    System.out.println("t get resource");
                }
            }
        }).start();
    }

    public void print() {
        new Thread(() -> {
            synchronized (object) {
                do {
                    if (i % 2 == 0) {
                        System.out.println(i++);
                    }

                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                while (i < 10);
                object.notifyAll();
            }
        }).start();


        new Thread(() -> {
            synchronized (object) {
                do {
                    if (i % 2 == 1) {
                        System.out.println(i++);
                    }
                    try {
                        object.notify();
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } while (i < 10);
                object.notifyAll();
            }
        }).start();

    }


    public void printNum(int num) {
        synchronized (this) {
//            if ()
        }
    }

    public static void print(int num) {
        for (int i = 31; i >= 0; i--) {
            System.out.print(((1 << i) & num) == 0 ? "0" : "1");
        }
        System.out.println();
    }


}
